<template>
  <view>
    <!-- 搜索组件 -->
    <view class="search">
      <my-search @click='toSearch'></my-search>
    </view>
    <!-- 轮播图 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(item,index) in swiperList" :key="index">
        <navigator :url="item.navigator_url" class="swiper-item">
          <image :src="item.image_src" mode="aspectFill"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 分类 -->
    <view class="cate">
      <view @click="toCate(item.name)" class="box" v-for="(item,index) in navList" :key="index">
        <image :src="item.image_src" mode=""></image>
      </view>
    </view>
    <!-- 楼层数据 -->
    <view class="floor">
      <view class="floor-item" v-for="(item,index) in floorList" :key="index">
        <!-- 楼层标题 -->
        <view class="title">
          <image :src="item.floor_title.image_src" mode="aspectFit"></image>
        </view>
        <!-- 楼层图片内容 -->
        <view class="imgbox">
          <view class="left">
            <image @click="toGoodList(item.product_list[0].navigator_url)" :src="item.product_list[0].image_src" :style="{width:item.product_list[0].image_width + 'rpx'}" mode="widthFix"></image>
          </view>
          <view class="right">
            <block v-for="(subitem,subindex) in item.product_list" :key="subindex">
              <image @click="toGoodList(subitem.navigator_url)" v-if="subindex !== 0" :src="subitem.image_src" :style="{width:subitem.image_width + 'rpx'}" mode="widthFix"></image>
            </block>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import mySearch from '../../components/my-search/my-search.vue'
  import mySetTabbar from '../../tools/setTabbar.js'
  export default {
    mixins:[mySetTabbar],
    props:{
      mySearch
    },
    data() {
      return {
        // 轮播图数据
        swiperList: [],
        // 获取分类数据
        navList: [],
        // 获取楼层数据
        floorList: []
      };
    },
    // 生命周期钩子,一打开页面就获取数据
    onLoad() {
      this.getSwiperList()
      this.getNavList()
      this.getFloorList()
    },
    methods: {
      // 点击跳转至搜索页面
      toSearch(){
        uni.navigateTo({
          url:'/pages/search/search'
        })
      },
      // 点击跳转至商品详情页面
      toGoodList(url){
        // console.log(url);
        // 从路径中分厘出参数
        let query = url.split('?')[1]
        // 路径跳转拼接地址
        uni.navigateTo({
          url:`/pages/good_list/good_list?${query}`
        })
      },
      // 获取楼层数据
      async getFloorList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/floordata')
        // console.log(res);
        if (res.meta.status !== 200) return uni.$showMsg()
        // 根据返回结果处理url地址
        // 通过双层 forEach 循环，处理 URL 地址
        // res.message.forEach(floor => {
        //   floor.product_list.forEach(prod => {
        //     prod.url = '/subpkg/goods_list/goods_list?' + prod.navigator_url.split('?')[1]
        //   })
        // })
        // 缓存数据
        this.floorList = res.message
      },
      // 点击事件
      toCate(name) {
        if (name === '分类') {
          uni.switchTab({
            url: '/pages/category/category'
          })
        }
      },
      // 获取分类数据
      async getNavList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/catitems')
        // console.log(res);
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        this.navList = res.message
      },
      // 获取轮播图数据
      async getSwiperList() {
        const res = await uni.$http.get('/api/public/v1/home/swiperdata')
        // console.log(res);
        // 判断请求状态
        if (res.data.meta.status === 200) {
          // 请求成功保存数据
          this.swiperList = res.data.message
        } else {
          // 请求失败提示错误信息
          uni.$showMsg()
        }
      }
    },

  }
</script>

<style lang="scss">
  swiper {
    height: 330rpx;

    .swiper-item,
    image {
      width: 100%;
      height: 100%;
    }
  }

  .cate {
    display: flex;
    justify-content: space-around;
    padding: 40rpx 0;

    .box {
      width: 160rpx;
      height: 180rpx;

      image {
        width: 100%;
        height: 100%;
      }
    }
  }

.floor{
  .floor-item{
    .title{
      image{
        height: 60rpx;
      }
    }
    .imgbox{
      display: flex;
      .left{
        margin-left: 17rpx;
      }
      .right{
        image{
          margin-left: 10rpx;
        }
        
      }
    }
  }
}
  .search{
    // 通过定位来实现吸顶效果
    position: sticky;
    // 吸顶位置
    top: 0;
    // 提高层级，防止被其他的覆盖
    z-index: 999;
  }
</style>
